home *** CD-ROM | disk | FTP | other *** search
/ Kit PC World De Ampliacion De Windows 95 / Kit PC World de ampliacion de Windows 95.iso / internet / sweeper / samples / olecon~1 / include / util.h < prev   
Text File  |  1995-11-25  |  6KB  |  141 lines

  1. //=--------------------------------------------------------------------------=
  2. // Util.H
  3. //=--------------------------------------------------------------------------=
  4. // Copyright  1995  Microsoft Corporation.  All Rights Reserved.
  5. //
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF 
  7. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  8. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A 
  9. // PARTICULAR PURPOSE.
  10. //=--------------------------------------------------------------------------=
  11. //
  12. // contains utilities that we will find useful.
  13. //
  14. #ifndef _UTIL_H_
  15.  
  16. #include "Globals.H"
  17.  
  18.  
  19. //=--------------------------------------------------------------------------=
  20. // Misc Helper Routines
  21. //=--------------------------------------------------------------------------=
  22. //
  23. HWND      GetParkingWindow(void);
  24. HINSTANCE GetResourceHandle(void);
  25.  
  26. //=--------------------------------------------------------------------------=
  27. // miscellaneous [useful] numerical constants
  28. //=--------------------------------------------------------------------------=
  29. // the length of a guid once printed out with -'s, leading and trailing bracket,
  30. // plus 1 for NULL
  31. //
  32. #define GUID_STR_LEN    40
  33.  
  34.  
  35. //=--------------------------------------------------------------------------=
  36. // allocates a temporary buffer that will disappear when it goes out of scope
  37. // NOTE: be careful of that -- make sure you use the string in the same or
  38. // nested scope in which you created this buffer. people should not use this
  39. // class directly.  use the macro(s) below.
  40. //
  41. class TempBuffer {
  42.   public:
  43.     TempBuffer(ULONG cBytes) {
  44.         m_pBuf = (cBytes <= 120) ? &m_szTmpBuf : HeapAlloc(g_hHeap, 0, cBytes);
  45.         m_fHeapAlloc = (cBytes > 120);
  46.     }
  47.     ~TempBuffer() {
  48.         if (m_pBuf && m_fHeapAlloc) HeapFree(g_hHeap, 0, m_pBuf);
  49.     }
  50.     void *GetBuffer() {
  51.         return m_pBuf;
  52.     }
  53.  
  54.   private:
  55.     void *m_pBuf;
  56.     // we'll use this temp buffer for small cases.
  57.     //
  58.     char  m_szTmpBuf[120];
  59.     unsigned m_fHeapAlloc:1;
  60. };
  61.  
  62. //=--------------------------------------------------------------------------=
  63. // string helpers.
  64. //
  65. // given and ANSI String, copy it into a wide buffer.
  66. // be careful about scoping when using this macro!
  67. //
  68. // how to use the below two macros:
  69. //
  70. //  ...
  71. //  LPSTR pszA;
  72. //  pszA = MyGetAnsiStringRoutine();
  73. //  MAKE_WIDEPTR_FROMANSI(pwsz, pszA);
  74. //  MyUseWideStringRoutine(pwsz);
  75. //  ...
  76. //
  77. // similarily for MAKE_ANSIPTR_FROMWIDE.  note that the first param does not
  78. // have to be declared, and no clean up must be done.
  79. //
  80. #define MAKE_WIDEPTR_FROMANSI(ptrname, ansistr) \
  81.     long __l##ptrname = (lstrlen(ansistr) + 1) * sizeof(WCHAR); \
  82.     TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  83.     MultiByteToWideChar(CP_ACP, 0, ansistr, -1, (LPWSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname); \
  84.     LPWSTR ptrname = (LPWSTR)__TempBuffer##ptrname.GetBuffer()
  85.  
  86. #define MAKE_ANSIPTR_FROMWIDE(ptrname, widestr) \
  87.     long __l##ptrname = (lstrlenW(widestr) + 1) * sizeof(char); \
  88.     TempBuffer __TempBuffer##ptrname(__l##ptrname); \
  89.     WideCharToMultiByte(CP_ACP, 0, widestr, -1, (LPSTR)__TempBuffer##ptrname.GetBuffer(), __l##ptrname, NULL, NULL); \
  90.     LPSTR ptrname = (LPSTR)__TempBuffer##ptrname.GetBuffer()
  91.  
  92. #define STR_BSTR   0
  93. #define STR_OLESTR 1
  94. #define BSTRFROMANSI(x)    (BSTR)MakeWideStrFromAnsi((LPSTR)(x), STR_BSTR)
  95. #define OLESTRFROMANSI(x)  (LPOLESTR)MakeWideStrFromAnsi((LPSTR)(x), STR_OLESTR)
  96. #define BSTRFROMRESID(x)   (BSTR)MakeWideStrFromResourceId(x, STR_BSTR)
  97. #define OLESTRFROMRESID(x) (LPOLESTR)MakeWideStrFromResourceId(x, STR_OLESTR)
  98. #define COPYOLESTR(x)      (LPOLESTR)MakeWideStrFromWide(x, STR_OLESTR)
  99. #define COPYBSTR(x)        (BSTR)MakeWideStrFromWide(x, STR_BSTR)
  100.  
  101. LPWSTR MakeWideStrFromAnsi(LPSTR, BYTE bType);
  102. LPWSTR MakeWideStrFromResourceId(WORD, BYTE bType);
  103. LPWSTR MakeWideStrFromWide(LPWSTR, BYTE bType);
  104.  
  105.  
  106. // takes a GUID, and a pointer to a buffer, and places the string form of the
  107. // GUID in said buffer.
  108. //
  109. int StringFromGuidA(REFIID, LPSTR);
  110.  
  111.  
  112. //=--------------------------------------------------------------------------=
  113. // registry helpers.
  114. //
  115. // takes some information about an Automation Object, and places all the
  116. // relevant information about it in the registry.
  117. //
  118. BOOL RegSetMultipleValues(HKEY hkey, ...);
  119. BOOL RegisterUnknownObject(LPSTR pszObjectName, REFCLSID riidObject);
  120. BOOL RegisterAutomationObject(LPSTR pszLibName, LPSTR pszObjectName, long lVersion, REFCLSID riidLibrary, REFCLSID riidObject);
  121. BOOL RegisterControlObject(LPSTR pszLibName, LPSTR pszObjectName, long lVersion, REFCLSID riidLibrary, REFCLSID riidObject, DWORD dwMiscStatus, WORD wToolboxBitmapId);
  122. BOOL UnregisterUnknownObject(REFCLSID riidObject);
  123. BOOL UnregisterAutomationObject(LPSTR pszLibName, LPSTR pszObjectName, long lVersion, REFCLSID riidObject);
  124. #define UnregisterControlObject UnregisterAutomationObject
  125. BOOL UnregisterTypeLibrary(REFCLSID riidLibrary);
  126.  
  127. // deletes a key in the registr and all of it's subkeys
  128. //
  129. BOOL DeleteKeyAndSubKeys(HKEY hk, LPSTR pszSubKey);
  130.  
  131.  
  132. //=--------------------------------------------------------------------------=
  133. // conversion helpers.
  134. //
  135. void        HiMetricToPixel(const SIZEL *pSizeInHiMetric, SIZEL *pSizeinPixels);
  136. void        PixelToHiMetric(const SIZEL *pSizeInPixels, SIZEL *pSizeInHiMetric);
  137.  
  138.  
  139. #define _UTIL_H_
  140. #endif // _UTIL_H_
  141.